package com.alageek.crowd.mvc.handler;

import com.alageek.crowd.entity.Role;
import com.alageek.crowd.service.api.RoleService;
import com.alageek.crowd.util.ResultEntity;
import com.github.pagehelper.PageInfo;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * @author alageek
 */
@Controller
@RequestMapping("/role")
public class RoleHandler {

    private final RoleService roleService;

    public RoleHandler(RoleService roleService) {
        this.roleService = roleService;
    }

    @ResponseBody
    @RequestMapping("/remove/roleIdArray.json")
    public ResultEntity<String> removeRoleArray(@RequestBody List<Integer> roleIdArray){
        roleService.removeRoleArray(roleIdArray);
        return ResultEntity.successWithoutData();
    }

    @ResponseBody
    @RequestMapping("/update.json")
    public ResultEntity<String> updateRole(Role role){
        roleService.updateRole(role);
        return ResultEntity.successWithoutData();
    }

    @ResponseBody
    @RequestMapping("/save.json")
    public ResultEntity<String> saveRole(Role role){
        roleService.saveRole(role);
        return ResultEntity.successWithoutData();
    }

    @PreAuthorize("hasRole('用户管理')")
    @ResponseBody
    @RequestMapping("/get/page/info.json")
    public ResultEntity<PageInfo<Role>> getPageInfo(
            @RequestParam(value = "keyword", defaultValue = "") String keyword,
            @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize){
        return ResultEntity.successWithData(roleService.getPageInfo(keyword, pageNum, pageSize));
    }

}
